/**
 * Created by tanbingqin on 2020/11/6.
 */
import { messages } from "share/common";
import React from "react";
import { Icon, Button, Row, Col, message, Input, Form } from "antd";
const FormItem = Form.Item;
import Condition from "components/condition";
const If = Condition.If;
import errorMessage from "share/errorMessage";
import subFormUtil from "components/template/sub-form/sub-form-util";
import "styles/components/template/sub-form/sub-form.scss";
import customField from "share/customField";
import travelUtil from "containers/request/travel-request/travelUtil";
let timer;
let ignoreDiff; //用于input这类的控件非英文输入法情况下的onChange问题

class SubFormRow extends React.Component {
  state = {};

  componentDidMount() {}

  componentWillUnmount() {
    if (timer) {
      clearTimeout(timer);
    }
  }

  componentWillReceiveProps() {}

  shouldComponentUpdate(nextProps, nextState) {
    return ignoreDiff || subFormUtil.isDiffRow(nextProps.fieldList, this.props.fieldList);
  }

  componentDidUpdate() {
    ignoreDiff = false;
  }

  //提供给父组件的调用方法 -- start --
  validateItemsValue = () => {
    let result = {};
    this.props.form.validateFields((err, values) => {
      if (!err) {
        result = { validateResult: true };
      } else {
        ignoreDiff = true;
        result = { validateResult: false };
      }
    });
    return result;
  };

  getCustFormValues = () => {
    const { fieldList } = this.props;
    let values = this.props.form.getFieldsValue();
    let custFormValues = JSON.parse(JSON.stringify(fieldList));
    custFormValues.map((item) => {
      Object.keys(values).map((key) => {
        if (key.split("|")[1] === item.fieldOID) {
          item = customField.formatFormValue(item, values[key]);
        }
      });
    });
    return custFormValues;
  };
  //提供给父组件的调用方法 -- end --

  //根据表单信息渲染控件
  renderCustom = (field, index, frontOID) => {
    const { getFieldDecorator } = this.props.form;
    //label
    let label = field.fieldName;

    //rules
    let maxLength = travelUtil.getMaxLength(field);
    let rules = [
      {
        required: field.required,
        message: messages("common.can.not.be.empty", {
          arg1: field.fieldName,
        }) /*{arg1} 不能为空*/,
      },
    ];
    maxLength &&
      rules.push({
        max: maxLength,
        message: messages("common.max.characters.length", {
          arg1: maxLength,
        }) /*最多输入{arg1}个字符*/,
      });

    //initialValue
    let fieldDefaultValue = {};

    return (
      <div key={index}>
        <FormItem formItemLayout="vertical" label={label || " "} key={`${frontOID}|${field.fieldOID}`} colon={!!label}>
          {getFieldDecorator(`${frontOID}|${field.fieldOID}`, {
            rules,
            valuePropName: field.messageKey === "switch" ? "checked" : "value",
            initialValue: customField.getInitialValue(field), //customField.getDefaultValue(field, fieldDefaultValue)
          })(
            customField.renderForm({
              field,
              fieldDefaultValue,
            })
          )}
        </FormItem>
      </div>
    );
  };

  render() {
    const { fieldList } = this.props;
    return (
      <div>
        {fieldList.map((field, index) => {
          return this.renderCustom(field, index, fieldList.frontOID);
        })}
      </div>
    );
  }

  static propTypes = {
    field: React.PropTypes.object, //子表格主控件
    fieldList: React.PropTypes.array, //子表格中的一行控件列表数据
    onChange: React.PropTypes.func,
  };

  static defaultProps = {
    field: {},
    fieldList: [],
    onChange: () => {},
  };
}

const WrappedSubFormRow = Form.create({
  onValuesChange(props, values) {
    ignoreDiff = true;

    //需要change完成再抛出onChange
    timer = setTimeout(() => {
      props.onChange(props.fieldList && props.fieldList.frontOID);
    }, 10);
  },
})(SubFormRow);
export default WrappedSubFormRow;
